package dwei.log
{
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	import mx.core.UIComponent;
	
	public class LogUI extends UIComponent implements ILogUI
	{
//============================================================================
//PROPERTY
//============================================================================
		private var _maxChanged:Boolean = false;
		private var _max:uint;
		private var _listChanged:Boolean = false;
		private const _msgList:Array = [];
//============================================================================
//CONSTRUCTOR
//============================================================================
		public function LogUI()
		{
			super();
			mouseEnabled = mouseChildren = false;
			
			_max = 100;
		}
//============================================================================
//OVERRIDE
//============================================================================
		override protected function commitProperties():void
		{
			super.commitProperties();
			if (_listChanged)
			{
				var tf:TextField;
				while (_msgList.length > 0)
				{
					tf = new TextField();
					tf.selectable = false;
					tf.autoSize = TextFieldAutoSize.LEFT;
					tf.htmlText = _msgList.shift();
					addChild(tf);
				}
				validateMax();
				_listChanged = false;
			}
			if (_maxChanged)
			{
				validateMax();
				_maxChanged = false;
			}
		}
		override protected function measure():void
		{
			super.measure();
			var tf:TextField;
			var index:int = numChildren - 1;
			measuredHeight = 0;
			while (index > -1)
			{
				tf = getChildAt(index--) as TextField;
				measuredWidth = Math.max(measuredWidth, tf.textWidth);
				measuredHeight += tf.textHeight;
			}
			invalidateDisplayList();
		}
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			var y:int = 0;
			var len:int = numChildren;
			var tf:TextField;
			for (var index:int = 0;index < len;index++)
			{
				tf = getChildAt(index) as TextField;
				tf.y = y;
				y += tf.textHeight;
			}
		}
		public function clear():void
		{
			while (numChildren > 0)
			{
				removeChildAt(0);
			}
			invalidateSize();
		}
		public function append(msg:String):void
		{
			_msgList.push(msg);
			_listChanged = true;
			invalidateProperties();
		}
//============================================================================
//PRIVATE
//============================================================================
		private function validateMax():void
		{
			while (numChildren > _max)
			{
				removeChildAt(0);
			}
			invalidateSize();
		}
//============================================================================
//GETTER/SETTER
//============================================================================
		public function get max():uint
		{
			return _max;
		}
		public function set max(value:uint):void
		{
			if (value == _max) return;
			_max = value;
			_maxChanged = true;
			invalidateProperties();
		}
	}
}